package com.study.message.job;

import com.pojo.entity.MessageLogEntity;
import com.pojo.entity.MessageTaskEntity;
import com.quartz.config.CustomJob;
import com.quartz.config.JobConstant;
import com.quartz.jobTask.MessageInfo;
import com.redis.queue.CacheConstant;
import com.redis.queue.RedisQueueUtils;
import com.study.mapper.MessageLogMapper;
import com.study.mapper.MessageTaskMapper;
import org.quartz.JobDetail;
import org.quartz.JobExecutionContext;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Component;

/**
 * (定时)执行实例Job
 * @author hewei
 */
@Component
public class MessageJob extends CustomJob {

    private MessageTaskMapper messageTaskMapper;

    private MessageLogMapper messageLogMapper;

    private RedisQueueUtils redisQueueUtils;

    public MessageJob(MessageTaskMapper messageTaskMapper,
                      MessageLogMapper messageLogMapper,
                      RedisQueueUtils redisQueueUtils){
        this.messageTaskMapper = messageTaskMapper;
        this.messageLogMapper = messageLogMapper;
        this.redisQueueUtils = redisQueueUtils;
    }

    @Override
    public void execute(JobExecutionContext context) {
        JobDetail jobDetail = context.getJobDetail();
        Long taskId = (Long)jobDetail.getJobDataMap().get(JobConstant.TASK_ID);
        Long logId = (Long)jobDetail.getJobDataMap().get(JobConstant.TOG_ID);
        MessageTaskEntity messageTaskEntity = messageTaskMapper.selectById(taskId);
        MessageInfo messageInfo = new MessageInfo();
        BeanUtils.copyProperties(messageTaskEntity,messageInfo);
        MessageInfo build = MessageInfo.builder().taskId(messageTaskEntity.getId()).build();
        redisQueueUtils.addMap(CacheConstant.MESSAGE_STEAM,build);
        MessageLogEntity messageLogEntity = new MessageLogEntity();
        messageLogEntity.setId(logId);
        messageLogEntity.setMeggageStatus(JobConstant.TIMING);
        messageLogMapper.updateById(messageLogEntity);
    }
}
